#ifndef COMPIPEWINDOW_H
#define COMPIPEWINDOW_H

#include <QMainWindow>
#include <QSerialPort>
#include <QTimer>

#include <stdint.h>
#include <glib.h>

#include <tvbuff.h>
#include <packet.h>
#include <proto.h>

#include "lua_dissect.h"

class ProtoTree;
class ByteViewTab;

class QSerialPort;

QT_BEGIN_NAMESPACE
namespace Ui { class ComPipeWindow; }
QT_END_NAMESPACE

class ComPipeWindow : public QMainWindow
{
    Q_OBJECT

public:
    ComPipeWindow(QWidget *parent = nullptr);
    ~ComPipeWindow();

private:
    void loadConfig();
    void loadDefaultConfig();
    bool readConfig();
    void saveConfig();

private slots:
    void scanActionTriggered();
    void startActionTriggered();
    void stopActionTriggered();
    void exitActionTriggered();
    void readyReadL();
    void readyReadR();

    void on_portL_currentTextChanged(const QString &arg1);
    void on_portR_currentTextChanged(const QString &arg1);

    void reloadLuaPlugins();

private:
    QSerialPort *openPort(const QString &port, qint32 baudRate, QSerialPort::DataBits dataBits, QSerialPort::Parity parity, QSerialPort::StopBits stopBits);
    void openEnable(bool enable);
    void checkIfSomeOneCom();

private:
    Ui::ComPipeWindow *ui;
    QDockWidget *m_prototreeDock;
    QDockWidget *m_byteviewDock;
    ProtoTree *m_prototree;
    ByteViewTab *m_byteview;

    QSerialPort *m_serialportL, *m_serialportR;

    qint64 m_l2r_cnt;
    qint64 m_r2l_cnt;

    QTimer m_sfTimL2R, m_sfTimR2L;  // slow flush
    QByteArray m_sfBufL2R, m_sfBufR2L;
    qint32 m_sfCntL2R, m_sfCntR2L;

    lua_dissector_t *ldt_;
};
#endif // COMPIPEWINDOW_H
